package Java8;

import com.company.LinkList.LIstTest;
import org.omg.CORBA.PUBLIC_MEMBER;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName DataStructure
 * @Description: TODO
 * @date ${DAT}22:58
 */
public class FilterApple {
    private static List<Apple> findGreenApple(List<Apple> apples){
        List<Apple> list = new ArrayList<>();

        for (Apple apple : apples) {
            if ("grenn".equals(apple.getColor())){
                list.add(apple);
            }
        }

        return list;
    }


    @FunctionalInterface
    public interface Predicate<T>{
        boolean test2(T t);
    }

    public static <T> List<T> filter(List<T> list,Predicate<T> p){
        List<T> results = new ArrayList<>();
        for (T t : list) {
            if (p.test2(t)){
                results.add(t);
            }
        }
        return results;
    }

    public interface ApplePredicate{
        boolean test(Apple apple);
    }

    public static List<Apple> filterApples(List<Apple> inventory,ApplePredicate p){//接口为参数
        List<Apple> result = new ArrayList<>();
        //List排序体验
        result.sort(new Comparator<Apple>() {
            @Override
            public int compare(Apple o1, Apple o2) {
                return o1.getColor().compareTo(o2.getColor());
            }
        });
        result.sort((o1, o2) -> o1.getColor().compareTo(o2.getColor()));

        for (Apple apple:inventory){
            if(p.test(apple)){
                result.add(apple);
            }
        }
        return result;
    }




    public static void main(String[] args) {
        List<Apple> list = Arrays.asList(new Apple("green",150),new Apple("yellow",200),new Apple("red",200));
        for (Apple apple : findGreenApple(list)) {
            System.out.println(apple);
        }
        List<Apple> redApples = filterApples(list, new ApplePredicate() {//匿名类
            @Override
            public boolean test(Apple apple) {
                return "red".equals(apple.getColor());
            }
        });
    }
}
